﻿using System;
using System.Collections.Generic;
using System.Text;
using Log4Nez.Interfaces;
using System.Xml;
using Log4Nez.Enumerations;
using System.Configuration;
using Log4Nez.Loggers;
using Log4Nez.Resources;

namespace Log4Nez.Factories
{
    internal static class EventLoggerFactory
    {
        public static ILogger CreateEventLogLogger(XmlNode loggerNode, LoggerType loggerType)
        {
            ILogger logger = null;
            
            string stringClassType = loggerNode.SelectSingleNode("PropertiesMapping") != null 
                ? loggerNode.SelectSingleNode("PropertiesMapping").Attributes["class"].Value 
                : null;

            if (!string.IsNullOrEmpty(stringClassType))
            {
                Dictionary<string, string> properties = new Dictionary<string, string>();
                XmlAttribute sourceNameAttribute = loggerNode.Attributes["sourceName"];
                if (sourceNameAttribute == null)
                    throw new ConfigurationErrorsException(string.Format(ExceptionMessages.XmlConfigAttributeRequired,"sourceName"));

                XmlAttribute eventSourceNameAttribute = loggerNode.Attributes["eventSourceName"];
                
                string eventSourceName = (eventSourceNameAttribute == null) ? "Application" : eventSourceNameAttribute.Value;

                
                XmlNodeList nodiProperty = loggerNode.SelectSingleNode("PropertiesMapping").HasChildNodes ? 
                    loggerNode.SelectSingleNode("PropertiesMapping").ChildNodes :
                    null;
                
                XmlNode originNode = loggerNode.SelectSingleNode("PropertiesMapping/Origin");
                XmlNode eventIdNode = loggerNode.SelectSingleNode("PropertiesMapping/EventId");
                XmlNode categoryIdNode = loggerNode.SelectSingleNode("PropertiesMapping/CategoryId");
                XmlNode instanceIdNode = loggerNode.SelectSingleNode("PropertiesMapping/InstanceId");
                XmlNode messageNode = loggerNode.SelectSingleNode("PropertiesMapping/Message");




                logger = new EventLogLogger(sourceNameAttribute.Value, eventSourceName, originNode.Attributes["property"].Value, eventIdNode.Attributes["property"].Value, categoryIdNode.Attributes["property"].Value, instanceIdNode.Attributes["property"].Value, messageNode.Attributes["property"].Value);
            }



            return logger;
        }
    }
}
